#include "rkmedia_module_config.h"

extern rkmediaInfo *rkmedia_config;
extern containerInfo *above_container_func;

rkmediaInfo::rkmediaInfo()
{
    printf("this is rkmediaInfo Init...\n");
}

rkmediaInfo::~rkmediaInfo()
{
    printf("this is rkmediaInfo destroyed...");
}

int rkmediaInfo::rkmedia_vi_init(RV1126_VI_CONFIG *vi)
{
    int ret = -1;
    VI_CHN_ATTR_S vi_attr = vi->attr;
    unsigned int vi_chn = vi->chn;
    ret = RK_MPI_VI_SetChnAttr(CAMERA_ID, vi_chn, &vi_attr);
    ret |= RK_MPI_VI_EnableChn(CAMERA_ID, vi_chn);
    if (ret)
    {
        printf("Create vi %d chn failed...\n", vi_chn);
        return -1;
    }
    else
    {
        printf("Create vi %d chn success...\n", vi_chn);
    }
    return 0;
}

int rkmediaInfo::rkmedia_venc_init(RV1126_VENC_CONFIG *venc)
{
    int ret = -1;
    VENC_CHN_ATTR_S venc_attr = venc->attr;
    unsigned int venc_chn = venc->chn;
    ret = RK_MPI_VENC_CreateChn(venc->chn, &venc_attr);
    if (ret)
    {
        printf("Create venc %d chn failed...\n", venc_chn);
        return -1;
    }
    else
    {
        printf("Create venc %d chn success...\n", venc_chn);
    }
    return 0;
}

int rkmediaInfo::rkmedia_rga_init(RV1126_RGA_CONFIG *rga)
{
    int ret = -1;
    RGA_ATTR_S rga_attr = rga->attr;
    unsigned int rga_chn = rga->chn;
    ret = RK_MPI_RGA_CreateChn(rga_chn, &rga_attr);
    if (ret)
    {
        printf("Create rga %d chn failed...\n", rga_chn);
        return -1;
    }
    else
    {
        printf("Create rga %d chn success...\n", rga_chn);
    }
    return 0;
}

int rkmediaInfo::rkmedia_ai_init(RV1126_AI_CONFIG *ai)
{
    int ret = -1;
    AI_CHN_ATTR_S ai_attr = ai->attr;
    unsigned int ai_chn = ai->chn;
    ret = RK_MPI_AI_SetChnAttr(ai_chn, &ai_attr);
    ret |= RK_MPI_AI_EnableChn(ai_chn);
    if (ret)
    {
        printf("Create ai %d chn failed...\n", ai_chn);
        return -1;
    }
    else
    {
        printf("Create ai %d chn success...\n", ai_chn);
    }
    return 0;
}

int rkmediaInfo::rkmedia_aenc_init(RV1126_AENC_CONFIG *aenc)
{
    int ret = -1;
    AENC_CHN_ATTR_S aenc_attr = aenc->attr;
    unsigned int aenc_chn = aenc->chn;
    ret = RK_MPI_AENC_CreateChn(aenc_chn, &aenc_attr);
    if (ret)
    {
        printf("Create aenc %d chn failed...\n", aenc_chn);
        return -1;
    }
    else
    {
        printf("Create aenc %d chn success...\n", aenc_chn);
    }
    return 0;
}

int rkmediaInfo::rkmedia_module_init()
{
    int ret = -1;
    RK_MPI_SYS_Init();

    RV1126_VI_CONFIG vi;
    vi.chn = 0;
    vi.attr.pcVideoNode = CAMERA_PATH;
    vi.attr.enPixFmt = IMAGE_TYPE_NV12;
    vi.attr.enBufType = VI_CHN_BUF_TYPE_MMAP;
    vi.attr.u32BufCnt = 3;
    vi.attr.u32Height = PIX_HEIGHT_SIZE;
    vi.attr.u32Width = PIX_WIDTH_SIZE;
    vi.attr.enWorkMode = VI_WORK_MODE_NORMAL;
    ret = rkmedia_config->rkmedia_vi_init(&vi);
    if (ret)
    {
        printf("Init vi %d chn failed...\n", vi.chn);
        return -1;
    }
    else
    {
        printf("Init vi %d chn success...\n", vi.chn);
        RV1126_VI_CONTAINER vi_container;
        vi_container.index = 0;
        vi_container.vi_chn = vi.chn;
        above_container_func->set_vi_container(0, &vi_container);
    }

    RV1126_VENC_CONFIG venc;
    venc.chn = 0;
    venc.attr.stVencAttr.enType = RK_CODEC_TYPE_H264;
    venc.attr.stVencAttr.enRotation = VENC_ROTATION_270;
    venc.attr.stVencAttr.imageType = IMAGE_TYPE_NV12;
    venc.attr.stVencAttr.u32PicHeight = PIX_HEIGHT_SIZE;
    venc.attr.stVencAttr.u32PicWidth = PIX_WIDTH_SIZE;
    venc.attr.stVencAttr.u32Profile = 66;
    venc.attr.stVencAttr.u32VirHeight = PIX_HEIGHT_SIZE;
    venc.attr.stVencAttr.u32VirWidth = PIX_WIDTH_SIZE;

    venc.attr.stRcAttr.enRcMode = VENC_RC_MODE_H264CBR;
    venc.attr.stRcAttr.stH264Cbr.u32Gop = 30;
    venc.attr.stRcAttr.stH264Cbr.u32BitRate = PIX_HEIGHT_SIZE * PIX_WIDTH_SIZE * 3;
    venc.attr.stRcAttr.stH264Cbr.u32SrcFrameRateDen = 1;
    venc.attr.stRcAttr.stH264Cbr.u32SrcFrameRateNum = 30;
    venc.attr.stRcAttr.stH264Cbr.fr32DstFrameRateDen = 1;
    venc.attr.stRcAttr.stH264Cbr.fr32DstFrameRateNum = 30;
    ret = rkmedia_config->rkmedia_venc_init(&venc);
    if (ret)
    {
        printf("Init venc %d chn failed...\n", venc.chn);
        return -1;
    }
    else
    {
        printf("Init venc %d chn success...\n", venc.chn);
        RV1126_VENC_CONTAINER venc_container;
        venc_container.index = 0;
        venc_container.venc_chn = venc.chn;
        above_container_func->set_venc_container(0, &venc_container);
    }

    RV1126_RGA_CONFIG rga;
    rga.chn = 0;
    rga.attr.stImgIn.imgType = IMAGE_TYPE_NV12;
    rga.attr.stImgIn.u32Height = PIX_HEIGHT_SIZE;
    rga.attr.stImgIn.u32Width = PIX_WIDTH_SIZE;
    rga.attr.stImgIn.u32VirStride = PIX_HEIGHT_SIZE;
    rga.attr.stImgIn.u32HorStride = PIX_WIDTH_SIZE;
    rga.attr.stImgIn.u32X = 0;
    rga.attr.stImgIn.u32Y = 0;

    rga.attr.stImgOut.imgType = IMAGE_TYPE_NV12;
    rga.attr.stImgOut.u32Height = 720;
    rga.attr.stImgOut.u32Width = 1280;
    rga.attr.stImgOut.u32VirStride = 720;
    rga.attr.stImgOut.u32HorStride = 1280;
    rga.attr.stImgOut.u32X = 0;
    rga.attr.stImgOut.u32Y = 0;

    rga.attr.u16Rotaion = 0;
    rga.attr.bEnBufPool = RK_TRUE;
    rga.attr.u16BufPoolCnt = 3;
    rga.attr.enFlip = RGA_FLIP_NULL;
    ret = rkmedia_config->rkmedia_rga_init(&rga);
    if (ret)
    {
        printf("Init rga %d chn failed...\n", rga.chn);
        return -1;
    }
    else
    {
        printf("Init rga %d chn success...\n", rga.chn);
        RV1126_RGA_CONTAINER rga_container;
        rga_container.index = 0;
        rga_container.rga_chn = rga.chn;
        above_container_func->set_rga_container(0, &rga_container);
    }

    RV1126_VENC_CONFIG low_venc;
    low_venc.chn = 1;
    low_venc.attr.stVencAttr.enType = RK_CODEC_TYPE_H264;
    low_venc.attr.stVencAttr.enRotation = VENC_ROTATION_270;
    low_venc.attr.stVencAttr.imageType = IMAGE_TYPE_NV12;
    low_venc.attr.stVencAttr.u32PicHeight = 720;
    low_venc.attr.stVencAttr.u32PicWidth = 1280;
    low_venc.attr.stVencAttr.u32Profile = 66;
    low_venc.attr.stVencAttr.u32VirHeight = 720;
    low_venc.attr.stVencAttr.u32VirWidth = 1280;

    low_venc.attr.stRcAttr.enRcMode = VENC_RC_MODE_H264CBR;
    low_venc.attr.stRcAttr.stH264Cbr.u32Gop = 30;
    low_venc.attr.stRcAttr.stH264Cbr.u32BitRate = 1280 * 720 * 3;
    low_venc.attr.stRcAttr.stH264Cbr.u32SrcFrameRateDen = 1;
    low_venc.attr.stRcAttr.stH264Cbr.u32SrcFrameRateNum = 30;
    low_venc.attr.stRcAttr.stH264Cbr.fr32DstFrameRateDen = 1;
    low_venc.attr.stRcAttr.stH264Cbr.fr32DstFrameRateNum = 30;
    ret = rkmedia_config->rkmedia_venc_init(&low_venc);
    if (ret)
    {
        printf("Init low_venc %d chn failed...\n", low_venc.chn);
        return -1;
    }
    else
    {
        printf("Init low_venc %d chn success...\n", low_venc.chn);
        RV1126_VENC_CONTAINER low_venc_container;
        low_venc_container.index = 1;
        low_venc_container.venc_chn = low_venc.chn;
        above_container_func->set_venc_container(1, &low_venc_container);
    }

    RV1126_AI_CONFIG ai;
    ai.chn = 0;
    ai.attr.pcAudioNode = "default";
    ai.attr.enAiLayout = AI_LAYOUT_NORMAL;
    ai.attr.u32Channels = 2;
    ai.attr.enSampleFormat = RK_SAMPLE_FMT_S16;
    ai.attr.u32NbSamples = 1024;
    ai.attr.u32SampleRate = 48000;
    ret = rkmedia_config->rkmedia_ai_init(&ai);
    if (ret)
    {
        printf("Init ai %d chn failed...\n", ai.chn);
        return -1;
    }
    else
    {
        printf("Init ai %d chn success...\n", ai.chn);
        RV1126_AI_CONTAINER ai_container;
        ai_container.index = 0;
        ai_container.ai_chn = ai.chn;
        above_container_func->set_ai_container(0, &ai_container);
    }

    RV1126_AENC_CONFIG aenc;
    aenc.chn = 0;
    aenc.attr.enCodecType = RK_CODEC_TYPE_AAC;
    aenc.attr.stAencAAC.u32Channels = 2;
    aenc.attr.stAencAAC.u32SampleRate = 48000;
    aenc.attr.u32Bitrate = 128000;
    aenc.attr.u32Quality = 1;
    ret = rkmedia_config->rkmedia_aenc_init(&aenc);
    if (ret)
    {
        printf("Init aenc %d chn failed...\n", aenc.chn);
        return -1;
    }
    else
    {
        printf("Init aenc %d chn success...\n", aenc.chn);
        RV1126_AENC_CONTAINER aenc_container;
        aenc_container.index = 0;
        aenc_container.aenc_chn = aenc.chn;
        above_container_func->set_aenc_container(0, &aenc_container);
    }

    return 0;
}